﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using ZedGraph;

namespace OilDBAPP
{
    public partial class home_Form : Form
    {
        private DataGridView reportView;

        public home_Form()
        {
            InitializeComponent();
        }

        private void home_Form_Load(object sender, EventArgs e)
        {
            if (reportView != null)
            {
                panel1.Controls.RemoveByKey(reportView.Name);
                reportView.Dispose();
            }

            reportView = createDataGridView("zyzbsl");
            reportView.DataSource = bindingSource1;
            reportView.Dock = DockStyle.Fill;
            reportView.AllowUserToResizeColumns = true;
            reportView.AllowUserToResizeRows = false;

            panel1.Controls.Add(reportView);

            GraphPane myPane = zedGraphControl1.GraphPane;

            // Set the Titles

            myPane.Title.Text = "My Test Bar Graph";
            myPane.XAxis.Title.Text = "Label";
            myPane.YAxis.Title.Text = "My Y Axis";

            // Make up some random data points

            string[] labels = { "Panther", "Lion", "Cheetah", "Cougar", "Tiger", "Leopard" };
            double[] y = { 100, 115, 75, 22, 98, 40 };
            double[] y2 = { 90, 100, 95, 35, 80, 35 };
            double[] y3 = { 80, 110, 65, 15, 54, 67 };
            double[] y4 = { 120, 125, 100, 40, 105, 75 };

            // Generate a red bar with "Curve 1" in the legend

            BarItem myBar = myPane.AddBar("Curve 1", null, y, Color.Red);
            myBar.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);

            // Generate a blue bar with "Curve 2" in the legend

            myBar = myPane.AddBar("Curve 2", null, y2, Color.Blue);
            myBar.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);

            // Generate a green bar with "Curve 3" in the legend

            myBar = myPane.AddBar("Curve 3", null, y3, Color.Green);
            myBar.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green);

            myPane.XAxis.MajorTic.IsBetweenLabels = true;
            myPane.XAxis.Scale.TextLabels = labels;
            myPane.XAxis.Type = AxisType.Text;

            // Fill the Axis and Pane backgrounds

            myPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
            myPane.Fill = new Fill(Color.FromArgb(250, 250, 255));

            // Tell ZedGraph to refigure the axes since the data have changed

            zedGraphControl1.AxisChange();
        }

        private DataGridView createDataGridView(String formid)
        {
            TreeView treeview = new TreeView();
            TreeNode tn;
            int deep;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"../../Data/forms.xml");

            XmlNode form = xmlDoc.SelectSingleNode("/root/form[@id='" + formid + "']"); //XPath表达式
            deep = Int32.Parse(((XmlElement)form).GetAttribute("columndeep"));
            GetData(((XmlElement)form).GetAttribute("sql"));

            if (deep == 2)
            {
                HeaderUnitView dgv = new HeaderUnitView();
                XmlNodeList columnList = form.SelectNodes("column");
                int i = 0;
                foreach (XmlNode column in columnList)
                {
                    tn = new TreeNode();
                    tn.Text = ((XmlElement)column).GetAttribute("text");
                    treeview.Nodes.Add(tn);

                    if (column.HasChildNodes)
                    {
                        XmlNodeList subColumnList = column.ChildNodes;
                        foreach (XmlNode subColumn in subColumnList)
                        {
                            tn = new TreeNode();
                            tn.Text = ((XmlElement)subColumn).GetAttribute("text");
                            treeview.Nodes[i].Nodes.Add(tn);

                            DataGridViewColumn dgvc = new DataGridViewTextBoxColumn();
                            dgvc.DataPropertyName = ((XmlElement)subColumn).GetAttribute("id");
                            dgv.Columns.Add(dgvc);
                        }
                    }
                    else
                    {
                        DataGridViewColumn dgvc = new DataGridViewTextBoxColumn();
                        dgvc.DataPropertyName = ((XmlElement)column).GetAttribute("id");
                        dgv.Columns.Add(dgvc);
                    }
                    i++;
                }

                dgv.ColumnTreeView = new TreeView[] { treeview };
                dgv.ColumnDeep = deep; //列深
                dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
                dgv.AutoGenerateColumns = false; //不自动增加列
                dgv.RowHeadersVisible = false; //行头不可见
                dgv.MultiSelect = false;
                dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgv.Columns[0].Frozen = true; //首列固定
                dgv.RefreshAtHscroll = true;
                return dgv;
            }
            else
            {
                DataGridView dgv = new DataGridView();
                XmlNodeList columnList = form.SelectNodes("column");
                foreach (XmlNode column in columnList)
                {
                    DataGridViewColumn dgvc = new DataGridViewTextBoxColumn();
                    dgvc.DataPropertyName = ((XmlElement)column).GetAttribute("id");
                    dgvc.Name = ((XmlElement)column).GetAttribute("text");
                    dgv.Columns.Add(dgvc);
                }
                dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
                dgv.AutoGenerateColumns = false;
                dgv.RowHeadersVisible = false;
                dgv.MultiSelect = false;
                dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                return dgv;
            }
        }

        private void GetData(string selectCommand)
        {
            try
            {
                String connectionString = "Data Source=.;Initial Catalog=AdventureWorksDW2008R2;Integrated Security=True";
                SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;
            }
            catch (SqlException)
            {
                MessageBox.Show("Cannot connect to the datasource!");
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Text == "重要指标速览")
            {
                if (reportView != null)
                {
                    panel1.Controls.RemoveByKey(reportView.Name);
                    reportView.Dispose();
                }

                reportView = createDataGridView("zyzbsl");
                reportView.DataSource = bindingSource1;
                reportView.Dock = DockStyle.Fill;
                reportView.AllowUserToResizeColumns = true;
                reportView.AllowUserToResizeRows = false;

                panel1.Controls.Add(reportView);
            }
            else
            {
                if (reportView != null)
                {
                    panel1.Controls.RemoveByKey(reportView.Name);
                    reportView.Dispose();
                }

                reportView = createDataGridView("test");
                reportView.DataSource = bindingSource1;
                reportView.Dock = DockStyle.Fill;
                reportView.AllowUserToResizeColumns = true;
                reportView.AllowUserToResizeRows = false;

                panel1.Controls.Add(reportView);
            }
        }
    }
}